\chapter{Analysing existing board games}
\label{chapter:Analysingexistingboardgames}
A Board game is really different from game to game, so it is difficult to give a specific definition of a board game. A common condition in board games are that the game is played on a specially designed board.
But the rules are different from game to game and sometimes a die or cards are used. A game can have a dynamic set of rules, so that there are more then one set of rules that can be used. Some board game rules look like each other. A good example is china chess and checkers, both of them have the element of jumping over pieces. In china chess you can jump over all pieces and in checkers you can only jump over the opponent's pieces. 

To better understand board games we wanted to take a look at a few existing ones, so we would understand what makes op a game. We will first take a look at Tic-Tac-Toe and then checkers.

\section{Tic-Tac-Toe}
We begin by taking a look at TTT(Tic-Tac-Toe) which is a simple board game played by two players. Each player picks which kind of piece they want. It can be eather X or O.  The players then take turns at placing a piece on the board shown in figure \ref{fig:TTT}. 

\begin{figure}[h!t]
	\begin{center}
	\includegraphics[width=0.7\textwidth]{./Analysis/tictactoe.jpg}
	\end{center}
	\caption{Tic Tae Toe}
	\label{fig:TTT}
\end{figure}

The goal of the game is to get three of your on pieces in a row.

So lates take a look at what makes up this game.
We need to be able to define a number of player, in this game there is to, we can then say
\begin{lstlisting}
Players = 2; //Defines the number of players to 2.
\end{lstlisting}
We also need the pieces, in this case also to X and O.
\begin{lstlisting}
Pieces = { X , O } //Defines the to types of pieces
\end{lstlisting}
We can describe the board by its dimension. 
\begin{lstlisting}
Board
{
	3*3; //Defines the board to me 3 x 3.
}
\end{lstlisting}
We now got the basics covered. Two players, two pieces and a board. We then need a way to decribe how this game is played. The rules of the game so to say. First we can look at the turn order. In TTT is fairly easy, on player places a piece
then the other and then keep switching until there isnt any more empty spaces. We can say:
\begin{lstlisting}
Turnes
{
	While(Games not finished) //If the game is not finished
	{
		Player 1; // First its player one.
	}
	While(Games not finished) //If the game is not finished
	{
		Player 2; // Then player two.
	}
}
\end{lstlisting}
So the turns depends on wether or not the game is won or the board is full. So we need some win or lose conditions. To win TTT you need three pieces in a row. 
\begin{lstlisting}
Winconditions //Defines the condition for a player to win.
{
	if(Player 1 has tre peices in a row) //If player one was three in row.
	{ 
		player 1 wins; //He/She wins
		player 2 loses; //Player 2 loses
		Game is finished; //And the game is finished
	}
	if(Player 2 has tre peices in a row) //Vice versa.
	{ 
		player 2 wins;
		player 1 loses;
		Game is finished;
	}
}
\end{lstlisting}
But if the board is full, and no players have managed to get three pieces in a row, the games is lost by both players, and thus we also need to define a lose conditition.
\begin{lstlisting}
Loseconditions //Defines the conditions for loseing
{
	if(board is full) //If there are no more available spaces
	{
		player1 & player2 has lost. //Both players have lost.
		Game is finished; //And the game is finished
	}
}
\end{lstlisting}
In some games, there is also a need for special rules that are unik to each game. There for i also need a rule part, but in the case of TTT it would not be needed.
\begin{lstlisting}
Rules
{

}
\end{lstlisting}
\section{Checkers}
Let's first find out what checkers is and the rules. Checkers is a litte more complex then Tic-Tak-Toe, because the board is bigger and each player have more pieces. The board is 8*8 cells and the two players each have 12 pieces each. Look at figure \ref{fig:checkers} for the setup. So only the gray cells on the board is used. A piece can only move forward one cell at a time diagonally to the right or left. If a opponent's piece is in the cell diagonally to the player's piece, the player is forced to jump diagonally over the opponent's piece. This means if player one need to jump diagonally to the right over the opponent's piece, player one's piece will be placed forward obliquely to the right from where the opponent's piece was placed. If the same piece can jump again, the piece is forced to jump again. If a piece 1 jumpes over another piece called piece 2, piece 2 gets removed from the board. It is imported to note that only one piece can jump in a turn, but the player choose which piece to move in the beginning of the turn. If a piece get all the way down to the other end of the board the piece becomes a king. A player can have all the kings he can get. A king can like a piece move diagonally to the left or right, but can also move back or forward diagonally. The rules we use a king can only move one cell at a time. If a king can jump a opponent's piece, the player is forced to jump with the king or another piece, if that piece can jump a opponent's piece. When a player get a king the players turn ends. This means that if a piece becomes a king, the king can't jump over a new piece in the same turn. A player wins the game if the opponent don't have any piece (kings included). If a player can't move the other player wins. The players can at any time call it a draw.
\begin{figure}[h!t]
	\begin{center}
	\includegraphics[width=0.7\textwidth]{./Analysis/checkers.jpg}
	\end{center}
	\caption{Checkers}
	\label{fig:checkers}
\end{figure}
Now we can write the game. Checkers is a two player game so here we define the number of players.
\begin{lstlisting}
Players = 2;
\end{lstlisting}
We need four different pieces for the two players. There are two set of normal pieces and there are two set of kings. We will write this with this line.
\begin{lstlisting}
Pieces = {NormalPlayerOnePiece, NormalPlayerTwoPiece, 
		  PlayerOneKing, PlayerTwoKing;}
\end{lstlisting}
Now that we have defined what pieces and players that is in the game, we need a way to assign the pieces to the player. This is done like this.
\begin{lstlisting}
Player 1 
{
		NormalPlayerOnePiece, PlayerOneKing;
}
Player 2 
{
		NormalPlayerTwoPiece, PlayerTwoKing;
}
\end{lstlisting}
As you know our board is a grid of 8*8, so this is easy to write.
\begin{lstlisting}
Board = {8*8;}
\end{lstlisting}
In checkers player one starts and then it is player two. They will keep playing until a player don't have any pieces (kings included) left. After each turn we check if a piece should become a king.  We writes this like this, where Game not finished will be defied later.
\begin{lstlisting}
Turnes
{
	While(Game not finished)
	{
		Round(1); //Round for Player 1
		for each(piece that the Player 1 have control over)
		{
			if(piece not a king and piece is 
			at the end of the board)
			{
			piece = king;
			}
		}
		Round(2); // Round for Player 2
		for each(piece that the Player 1 have control over)
		{
			if(piece not a king and piece is 
			at the end of the board)
			{
				piece = king;
			}
		}
	}
}
\end{lstlisting}
Each turn a player can move one piece, but we need to check if there are any pieces that is forced to jump. If a set of piece is forced to jump the player can only choose those pieces. So lets take a look at a round.
\begin{lstlisting}
Round(int playerNum)
{
	for each(piece that the player have control over)
	{
		Pieces[] piecesFoced;
		if(piece diagonally cell right == 
		Blocked or piece diagonally cell left ==
		Blocked)
		{
			piecesFoced.add(piece);
		}
	}		
	If(piecesFoced.lenght > 0) // are there pieces that are forced?
	{
		If (piece selected in piecesFoced) 
		// player want to move a piece.
		{
			Move piece;
		}
	}
	else
	{
		if(piece selected belongs to the player)
		{
			Move piece;
		}
	}
}
\end{lstlisting}
Lets take a closer look at the code above. The first thing we do is checking through all the pieces that belongs to the player. We do this to find all the pieces that is forced to jump. If we found any
of the player's pieces that is forced to jump, we check if the piece the player want to move is one
of those. If we didn't found any of the player's pieces that is forced to jump, we check if the piece the player want to move belongs to the player.
All we need now is a win condition. This is surprisingly simpler then Tic-Tak-Toe, the only thing we need to check is if one of the two players, don't have any pieces left.
\begin{lstlisting}
Wincondition
{
	if(NormalPlayerOnePiece.Count <= 0 and PlayerOneKing.Count > 0)
	{
		Player 1 loses;
		Player 2 wins;
		Game not finished = false;
	}
	else if(NormalPlayerTwoPiece.Count > 0 and PlayerTwoKing.Count <= 0)
	{
		Player 1 wins;
		Player 2 loses;
		Game not finished = false;
	}
}    
\end{lstlisting}
\begin{lstlisting}
Losecondition
{
	for each(player)
	{
		if(Round[1] is true)
		{
			if(player can't move)
			{
				Player 1 loses;
				Player 2 wins;
				Game not finished = false;
			}
		}

		if(Round[2] is true)
		{
			if(player can't move)
			{
				Player 1 wins;
				Player 2 loses;
				Game not finished = false;
			}
		}
	}
}
\end{lstlisting}
\section{Ludo}
\begin{figure}[h!t]
	\begin{center}
	\includegraphics[width=0.7\textwidth]{./Analysis/ludo.png}
	\end{center}
	\caption{Ludo}
	\label{fig:ludo}
\end{figure}
Unlike checkers and Tic-Tak-Toe Ludo need special cells on the board. The star, the globus and the home cell, these diffrent cell will need to be desribed in the in the board section. As an example we will take a look at how to decribe the board and how to define what a star is.
\begin{lstlisting}
Board //Defining the board
{
	14*14 //Its of size 14 * 14
	Exclude //But not all cells are used
	{
	1*1-1*6; //So we need to remove the first six cells in line one.
	1*9-1*14;//And we need to remove the last six cells in line one.
	2*1-2*6; //And so on.
	2*9-2*14;
	...
	5*1-5*6;
	5*9-5*14;
	8*1-8*6;
	8*9-8*14;
	...
	14*1-14*6;
	14*9-14*14;
	}

	Specialfield star //Then there are a few special cells. First one being the star. 
	{
		fieldmark = "star"; //It need to star mark on it.
		fieldproperties //And it does a specific job.
		{
			if(player just jumped == false and player is on field) //If a player places a piece on this cell.
			{
				move player to next star. //He moves to the next.
				player just jumped == true; // To insure the player dosent keep jumping.
			}
		}
		filedstomark //Then we need to mark the cells where the star is.
		{
			1*8, 7*9,  ... 14*7;
		}
	}
}
\end{lstlisting}
No we got a small sample size of the ludo board, we can look at the a small sample of rules in ludo. In ludo a players rolls a six sided die, and depending on the die he or she takes action. If a player dont have any peices out of the startarea, the player will ned a six to get a peice out of the starting area and on to the globe marked with the same color as the players peices. Ones a players has one peices out of the startingarea and rolles a die, the player can move the amount of the fields that corresponds to the number of eyes on the die or in case of a six, choose to get another peices in play. 
\begin{lstlisting}
Rules //Defines the special rules in the game.
{
	Moving //Defines how to move
	{
		Switch(dices) //A switch to handle how many eyes a player roll with the die.
		{
			case 1: //If we gets one eye
			move 1 a piece field; // Move one
			...
			case 6: //But if he/she gets a six.
			if(peices in play < 4) //We can choose to get another piece in play or move six spaces.
			{
				move piece from startingarea to 
				field playerHomeGlobe;
				
				or move a piece 6 field;
			}
			else // If the player have the maximum number of pices allowed in play
			{
				move a piece 6 field; //He can only move.
			}
		}
	}
}
\end{lstlisting}
This is just a sample, of cause to define the entire ludo game requires more rules, and also extending the rules above, the rules above would need to talke into account stars, other globes and other players. 
